package org.example.callback;

import com.github.dockerjava.api.command.BuildImageResultCallback;
import com.github.dockerjava.api.model.BuildResponseItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.Closeable;
import java.util.LinkedList;
import java.util.List;

public class BuildLogCallback extends BuildImageResultCallback {

    private static final Logger logger = LoggerFactory.getLogger(BuildLogCallback.class);

    List<String> logList = new LinkedList<>();

    public String imageName;

    public BuildLogCallback(String imageName) {
        this.imageName = imageName;
    }

    @Override
    public void onStart(Closeable stream) {
        logger.info("-------------镜像名称：「" + imageName + "」开始构建---------------");
        super.onStart(stream);
    }

    @Override
    public void onNext(BuildResponseItem item) {
        if (StringUtils.hasText(item.getStream())){
            //去除掉用不到的null
            logger.info(item.getStream());
        }
        super.onNext(item);
    }
    @Override
    public void onComplete() {
        logger.info("-------------镜像名称：「" + imageName + "」构建完成---------------");
        super.onComplete();
    }

    @Override
    public void onError(Throwable throwable) {
        logger.error("-------------镜像名称：「" + imageName + "」构建失败---------------");
        super.onError(throwable);
    }

    public List<String> getLogList() {
        return logList;
    }
}